package com.practice.J2E.Test;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author kayn
 * @create 2023-09-07 下午 02:24
 */
public class StreamTest {

    public static void main(String[] args) {

        ArrayList<DateTime> arrayList = CollUtil.newArrayList(DateUtil.parse("2023-11-02 00:00:00"), DateUtil.parse("2023-11-02 00:00:01"), DateUtil.parse("2023-11-02 00:00:03"), DateUtil.parse("2023-11-02 00:00:02"));

//        ArrayList<DateTime> arrayList = CollUtil.newArrayList();

        DateTime first = arrayList.stream().max(Comparator.naturalOrder()).orElse(null);

        System.out.println(first);

        /**
         *
         * 集合
         * 单列
         * 顶层接口：Collection
         *
         *
         * 双列
         * 顶层接口：Map
         *
         * @author kayn
         * @date 2023/9/8 19:16
         */


        List<Integer> numList = CollUtil.newArrayList(2, 1, 6, 3, 4, 5);

        //Stream流 + 函数式接口 + 匿名内部类 + Lambda表达式(包含简化) + 方法引用
        List<Integer> collect = numList.stream()
                .filter(new Predicate<Integer>() {

                    @Override
                    public boolean test(Integer num) {

                        return num <= 5;

                    }

                })
                .sorted((o1,o2) -> {
                    return o1-o2;
                })
                .map(num -> num * 2)
                .peek(System.out::println)
                .peek(StreamTest.print())
                .collect(Collectors.toList());

        System.out.println(collect);

        List<Integer> listA = CollUtil.newArrayList( 1, 2, 3, 4, 5);

        List<Integer> listB = CollUtil.newArrayList(6, 7, 8, 9, 0);

        ArrayList<List<Integer>> lists = CollUtil.newArrayList(listA, listB);

        List<Object> collect1 = lists.stream().flatMap(Collection::stream).collect(Collectors.toList());

        System.out.println(collect1);

    }

    public static <T> Consumer<? super T> print(){

        return new Consumer<T>() {
            @Override
            public void accept(T t) {

                System.out.println(t);

            }
        };

    }

}
